package com.hunan.api.catv.service.biz.member.open;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hunan.api.catv.common.CacheKey;
import com.hunan.api.catv.common.ConstantsMsg;
import com.hunan.api.catv.entity.ServiceResult;
import com.hunan.api.catv.entity.po.MemberOpenidInfo;
import com.hunan.api.catv.entity.vo.MemberVo;
import com.hunan.api.catv.service.ResultService;
import com.hunan.api.catv.service.biz.member.MemberBiz;
import com.hunan.api.catv.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * @Author: wangchao
 * @Date: 2020/10/27 14:56
 */
@Service
public class MemberOpenBiz extends ResultService {

    @Autowired
    MemberBiz memberBiz;

    @Autowired
    RedisUtils redisUtils;

    /**
     * 通过会员id和会员类型返回openid
     *
     * @param memberId   会员id
     * @param memberType 会员类型
     */
    public String getMemberThirdOpenId(Integer memberId, String memberType) {
        MemberVo info = memberBiz.info(memberId);
        if (info == null) {
            return null;
        }
        String memberThirdInfoKey = CacheKey.member_third_openid + ":" + memberId;
        String openId = String.valueOf(redisUtils.get(memberThirdInfoKey));
        if (!StringUtils.isEmpty(openId) && !"null".equals(openId)) {
            return openId;
        }
        //根据类型和会员id查询是否存在
        MemberOpenidInfo memberOpenidInfo = new MemberOpenidInfo().selectOne(new QueryWrapper<MemberOpenidInfo>()
                .eq(MemberOpenidInfo.MEMBER_ID, memberId)
                .eq(MemberOpenidInfo.MEMBER_THIRD_TYPE, memberType));
        if (memberOpenidInfo != null) {
            openId = memberOpenidInfo.getMemberOpenId();
        } else {
            openId = IdUtil.simpleUUID();
            boolean insert = new MemberOpenidInfo().setMemberId(memberId).setMemberThirdType(memberType).setMemberOpenId(openId).setCreateTime(new Date()).setUpdateTime(new Date()).insert();
            if (!insert) {
                return null;
            }
        }
        redisUtils.set(memberThirdInfoKey, openId, 7200);
        return openId;
    }

    /**
     * 通过openId获取会员信息
     */
    public MemberVo getMemberInfoByThirdOpenId(String openId) {
        MemberOpenidInfo memberOpenidInfo = new MemberOpenidInfo().selectOne(new QueryWrapper<MemberOpenidInfo>().eq(MemberOpenidInfo.MEMBER_OPEN_ID, openId));
        if (memberOpenidInfo != null) {
            return memberBiz.info(memberOpenidInfo.getMemberId());
        }
        return null;
    }

    /**
     * 修改会员扩展信息
     */
    public boolean updateMemberOpenInfo(MemberOpenidInfo memberOpenidInfo) {
        MemberOpenidInfo memberOpenidInfo1 = new MemberOpenidInfo().selectOne(new QueryWrapper<MemberOpenidInfo>()
                .eq(MemberOpenidInfo.MEMBER_ID, memberOpenidInfo.getMemberId())
                .eq(MemberOpenidInfo.MEMBER_THIRD_TYPE, memberOpenidInfo.getMemberThirdType()));
        if (memberOpenidInfo1 != null) {
            memberOpenidInfo.setMemberThirdOpenId(memberOpenidInfo.getMemberThirdOpenId()).setId(memberOpenidInfo1.getId()).setUpdateTime(new Date()).updateById();
            return true;
        }
        return false;
    }

    /**
     * 映射会员和极光
     */
    @Transactional(rollbackFor = Exception.class)
    public ServiceResult saveMemberAndJgInfo(MemberOpenidInfo memberOpenidInfo, Integer memberId) {
        boolean flag = false;
        if (memberId == null) {
            return buildOK(ConstantsMsg.member_login_fail);
        }
        MemberVo info = memberBiz.info(memberId);
        if (info == null) {
            return buildOK(ConstantsMsg.getUserInfo_fail);
        }
        //查询是否绑定
        MemberOpenidInfo memberOpenidInfo1 = new MemberOpenidInfo().selectOne(new QueryWrapper<MemberOpenidInfo>()
                .eq(MemberOpenidInfo.MEMBER_THIRD_TYPE, memberOpenidInfo.getMemberThirdType())
                .eq(MemberOpenidInfo.MEMBER_THIRD_OPEN_ID, memberOpenidInfo.getMemberThirdOpenId())
        );
        if (memberOpenidInfo1 == null) {
            flag = memberOpenidInfo.setMemberId(memberId).setMemberThirdType(memberOpenidInfo.getMemberThirdType()).setMemberOpenId(IdUtil.simpleUUID()).setMemberThirdOpenId(memberOpenidInfo.getMemberThirdOpenId()).setCreateTime(new Date()).insert();
        } else {
            flag = memberOpenidInfo1.setMemberId(memberId).setUpdateTime(new Date()).updateById();
        }
        if (flag) {
            return buildOK(ConstantsMsg.admin_add_success);
        }
        return buildFail(ConstantsMsg.admin_add_fail);
    }
}
